﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WEALTH
{
    public partial class MemberDetail : ClientBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[SessionKeys.CURRENT_MEMBER] == null)
            {
                Response.Redirect("index.aspx", true);
                return;
            }
            if (!IsPostBack)
            {
                MemberCurrent currentMember = (MemberCurrent)Session[SessionKeys.CURRENT_MEMBER];
                txt_UserName.Text = currentMember.M_Name;
                txt_Email.Text = currentMember.M_Email;
                txt_Phone.Text = currentMember.M_Phone;
                txt_RealName.Text = currentMember.M_RealName;
                txt_IDCard.Text = currentMember.M_Idcard;
                ddlidcardType.SelectedItem.Text = currentMember.M_IdcardType;
            }

        }

        protected void But_Region_Click(object sender, EventArgs e)
        {
            if (Session[SessionKeys.CURRENT_MEMBER] == null)
            {
                Response.Redirect("index.aspx", true);
                return;
            }

            if (CheckRegData())
            {
                MemberCurrent currentMember = new MemberCurrent();
                currentMember.Member_ID = ((MemberCurrent)Session[SessionKeys.CURRENT_MEMBER]).Member_ID;
                currentMember.M_Name = SpecialCharHandler.HtmlEncode(((MemberCurrent)Session[SessionKeys.CURRENT_MEMBER]).M_Name);
                if (txt_PassWord.Text.Trim().Length > 0)
                {
                    currentMember.M_Password = AppFunction.Encrypt(txt_PassWord.Text);
                }
                currentMember.M_Phone = txt_Phone.Text;
                currentMember.M_Email = txt_Email.Text;
                currentMember.M_RealName = txt_RealName.Text;
                currentMember.M_Idcard = txt_IDCard.Text;
                currentMember.M_IdcardType = ddlidcardType.SelectedItem.Text;

                Session[SessionKeys.REG_MEMBER] = currentMember;
                ExcuteDao dao = new ExcuteDao();
                if (dao.Member_UpdateFromMember(currentMember) < 0)
                {
                    ShowMsg("会员资料修改成功！", 0);

                 ((MemberCurrent)Session[SessionKeys.CURRENT_MEMBER]).M_Phone = txt_Phone.Text;
                 ((MemberCurrent)Session[SessionKeys.CURRENT_MEMBER]).M_Email = txt_Email.Text;
                 ((MemberCurrent)Session[SessionKeys.CURRENT_MEMBER]).M_RealName = txt_RealName.Text;
                 ((MemberCurrent)Session[SessionKeys.CURRENT_MEMBER]).M_Idcard = txt_IDCard.Text;
                 ((MemberCurrent)Session[SessionKeys.CURRENT_MEMBER]).M_IdcardType = ddlidcardType.SelectedItem.Text;                   
                }
            }
        }

        private bool CheckRegData()
        {
            if (ddlidcardType.SelectedValue.Equals("0"))
            {
                if (!AppFunction.CheckIDCard(txt_IDCard.Text))
                {
                    ShowMsg("身份证号不正确，请确认！", 1);
                    txt_IDCard.Focus();
                    return false;
                }
            }

            return true;
        }
    }
}